package com.atguigu.spring.service.impl;

import com.atguigu.spring.model.UserBalance;
import com.atguigu.spring.service.CashService;
import com.atguigu.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * @author mi
 * @time 2022/4/17 23:06
 */
@Component
public class CashServiceImpl implements CashService {


	@Autowired
	private UserService userService;
	/**
	 *  创建用户并创建账户余额
	 *
	 *   @param name
	 * 	@param balance
	 * 	@return
	 */
	@Transactional(propagation= Propagation.REQUIRED, rollbackFor = Exception.class)
	public void addUserBalanceAndUser(String name, BigDecimal balance) {
		System.out.println("[addUserBalanceAndUser] begin!!!");
		//1.新增用户
		userService.addUser(name);
		//2.新增用户余额
		UserBalance userBalance = new UserBalance();
		userBalance.setName(name);
		userBalance.setBalance(new BigDecimal(1000));
		this.addUserBalance(userBalance);
		System.out.println("[addUserBalanceAndUser] end!!!");
	}


	@Override
	public void addUserBalance(UserBalance userBalance) {
		System.out.println("[addUserBalance] !!!");
	}
}
